package com.song.NettyTest;

import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.LongAdder;

public class test{
    @Test
    public void ssest() {
        String s1 = "javaEE";
        String s2 = "hadoop";

        String s3 = "javaEEhadoop";
        String s4 = "javaEE" + "hadoop";//在class文件中显示的是s4 = "javaEEhadoop"

//如果拼接符号的前后出现了变量，则相当于在堆空间中new String()，具体的内容为拼接的结果
        String s5 = s1 + "hadoop";
        String s_5 = s1 + "hadoop";
        String s6 = "javaEE" + s2;
        String s7 = s1 + s2;
//上述只有s3与s4地址相同，其它全不相同

        String s8 = s6.intern();// s3 == s8 为true
        System.out.println(s5 == s_5);//false

//        LongAdder

        String str1 = new String("1") + new String("1");
        str1.intern();
        String str2 = "11";
        System.out.println(str1 == str2);

        String str3 = new String("2") + new String("2");

        str3.intern();

        String str4 = "22";

        System.out.println(str3 == str4);

        ThreadLocal<Object> threadLocal = new ThreadLocal<>();
        threadLocal.set("abc");
    }

    @Test
    public void testIntern() {
        String s = new String("1");
        String s2 = "1";
        s.intern();
        System.out.println(s == s2);
        String s3 = new String("a") + new String("a");
        s3.intern();
        String s4 = "aa";
        System.out.println(s3 == s4);
    }


    @Test
    public void test2() throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> completableFuture = testCom("1111");
        // 获取任务结果，如果没有完成会一直阻塞等待
        int result = completableFuture.get();
        System.out.println("计算结果:" + result);
    }

    private CompletableFuture<Integer> testCom(String a) {
        CompletableFuture<Integer> completableFuture = new CompletableFuture<>();
//        new Thread(() -> {
//            // 模拟执行耗时任务
//            System.out.println("task doing...");
//            try {
//                Thread.sleep(3000);
//                a += 2;
//            } catch (Exception e) {
//                // 告诉completableFuture任务发生异常了
//                completableFuture.completeExceptionally(e);
//            }
//            // 告诉completableFuture任务已经完成
//            completableFuture.complete(a);
//        }).start();

//        int b = testa(a);
        int b  = 1;
        completableFuture.complete(b);
        return completableFuture;
    }

    private int testa(String a) {
        //a = a * 8 + 2;
        //a = "10";
        String s = "888";
        //s = s + String.valueOf(a);
        int c = Integer.parseInt(s);
        return c;
    }

    @Test
    public void testBinaryDiv() {
        int divident = Integer.MIN_VALUE;
        int divisor = 1;
        System.out.println(divide(divident, divisor));
    }

    private int divide(int divident, int divisor) {
        if(divident == 0) return 0;
        if(divisor == Integer.MIN_VALUE && divisor == -1) {
            return Integer.MAX_VALUE;
        }

        boolean negFlag = (divident ^ divisor) < 0;

        long a = Math.abs((long)divident);
        long b = Math.abs((long)divisor);

        int res = 0;
        for(int i = 31;i >= 0;i--) {
            if((a >> i) >= b) {
                res += 1 << i;
                a -= b << i;
            }
        }
        return negFlag ? -res : res;
    }

    @Test
    public void testSort() {
        int[] arr = {12,32,34,15,2,6,3,45,26,37};
        heapSort(arr);
        for(int num : arr) {
            System.out.print(num + "\t");
        }
    }

    private void heapSort(int[] arr) {
        int len = arr.length - 1;
        for(int i = len / 2 - 1;i >= 0;i--) {
            adjustHeap(arr, i, len);
        }

        for(int i = len;i > 0;i--) {
            swap(arr, 0, i);
            adjustHeap(arr, 0, i - 1);
        }
    }

    private void adjustHeap(int[] arr, int start, int end) {
        int tmp = arr[start];
        for(int sonIndex = start * 2 + 1;sonIndex <= end;sonIndex = sonIndex * 2 + 1) {
            if(sonIndex + 1 <= end && arr[sonIndex + 1] > arr[sonIndex]) {
                sonIndex++;
            }

            if(arr[sonIndex] > tmp) {
                swap(arr, start, sonIndex);
                start = sonIndex;
            } else {
                break;
            }
        }
    }

    private void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}
